﻿using System.Reactive.Disposables;
using System.Windows.Forms;
using ReactiveUI;

namespace ReactiveUIDemo;

public partial class MainWindow : Form, IViewFor<AppViewModel>
{
    public MainWindow()
    {
        InitializeComponent();

        ViewModel = new AppViewModel();

        // We create our bindings here. These are the code behind bindings which allow
        // type safety. The bindings will only become active when the Window is being shown.
        // We register our subscription in our disposableRegistration, this will cause
        // the binding subscription to become inactive when the Window is closed.
        // The disposableRegistration is a CompositeDisposable which is a container of
        // other Disposables. We use the DisposeWith() extension method which simply adds
        // the subscription disposable to the CompositeDisposable.
        this.WhenActivated(disposableRegistration =>
        {
            this.OneWayBind(ViewModel,
                    viewModel => viewModel.IsAvailable,
                    view => view.searchResultsListBox.Visible)
                .DisposeWith(disposableRegistration);

            this.OneWayBind(ViewModel,
                    viewModel => viewModel.SearchResults,
                    view => view.searchResultsListBox.Controls,
                    vmToViewConverterOverride: new ListBoxItemConverter())
                .DisposeWith(disposableRegistration);

            this.Bind(ViewModel,
                    viewModel => viewModel.SearchTerm,
                    view => view.searchTextBox.Text)
                .DisposeWith(disposableRegistration);
        });
    }

    object IViewFor.ViewModel
    {
        get => ViewModel;
        set => ViewModel = (AppViewModel)value;
    }

    public AppViewModel ViewModel { get; set; }
}
